package cn.wolfcode.web.controller;


import cn.wolfcode.common.exception.BusinessException;
import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.feign.AliPayFeignApi;
import cn.wolfcode.service.IOrderInfoService;
import cn.wolfcode.web.msg.SeckillCodeMsg;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;


@RestController
@RequestMapping("/orderPay")
@RefreshScope
public class OrderPayController {
    @Autowired
    private IOrderInfoService orderInfoService;
    @Autowired
    private AliPayFeignApi aliPayFeignApi;
    @Value("${pay.frontEndPayUrl}")
    private String frontEndPayUrl;

    @RequestMapping("/alipay")
    public Result<String> alipay(String orderNo, int type) {
        if (StringUtils.isEmpty(orderNo) || StringUtils.isEmpty(type + "")) {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        if (OrderInfo.PAYTYPE_ONLINE.equals(type)) {
            //在线支付
            String onlineString = orderInfoService.onlinePayment(orderNo, type);
            return Result.success(onlineString);
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(type)) {
            //积分支付
            orderInfoService.IntegralToPay(orderNo, type);
        } else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");
    }

    /**
     * 展示支付订单数据
     *
     * @param params
     * @param response
     */
    @RequestMapping("/return_url")
    public void returnUrl(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        System.out.println("同步回调---》" + new Date());
        Result<Boolean> result = aliPayFeignApi.signCheck(params);
        //如果验签失败，跳到失败页面
        if (result != null || !result.hasError()) {
            if (result.getData()) {
                //如果验签成功
                //重定向到订单详情页面
                String orderNo = params.get("out_trade_no");
                response.sendRedirect(frontEndPayUrl + orderNo);
            } else {
                response.sendRedirect("http://localhost/50x.html");
            }
        }
    }

    @RequestMapping("/notify_url")
    public void notifyUrl(@RequestParam Map<String, String> params, HttpServletResponse response) throws IOException {
        System.out.println("异步回调---》" + new Date());
        Result<Boolean> result = aliPayFeignApi.signCheck(params);
        String orderNo = params.get("out_trade_no");
        PrintWriter writer = response.getWriter();
        if (result != null || !result.hasError()) {
            if (result.getData()) {
                //执行支付成功业务
                orderInfoService.paySuccess(orderNo);
                writer.print("success");
            } else {
                writer.print("fail");
            }
        }
    }

    @RequestMapping("/refund")
    public Result<String> refund(String orderNo) {
        if (StringUtils.isEmpty(orderNo)) {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        OrderInfo orderInfo = orderInfoService.find(orderNo);
        if (OrderInfo.PAYTYPE_ONLINE.equals(orderInfo.getPayType())) {
            //走在线退款流程
            orderInfoService.refundOnline(orderInfo);
        } else if (OrderInfo.PAYTYPE_INTERGRAL.equals(orderInfo.getPayType())) {
            //走积分退款流程
            orderInfoService.IntegralRefund(orderInfo);
        } else {
            throw new BusinessException(SeckillCodeMsg.OPT_ERROR);
        }
        return Result.success("");
    }
}
